﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

namespace Eagle.Web
{
    /// <summary>
    /// 页面设置类
    /// </summary>
    public class PageSettings
    {
        private PageSettings() { }
        private static PageSettings _instance = new PageSettings();
        /// <summary>
        /// 页面设置类实例
        /// </summary>
        public static PageSettings Instance
        {
            get {
                return _instance;
            }
        }

        /// <summary>
        /// 在页面头部添加JavaScript链接
        /// </summary>
        /// <param name="page">要加载的页面实例</param>
        /// <param name="src">JS全路径</param>
        /// <param name="defer">是否延迟解析JS</param>
        /// <returns></returns>
        public void AppendJavascript(Page page,string src, bool defer)
        {
            string javascript;
            if(defer)
                javascript="<script language=\"javascript\" type=\"text/javascript\" src=\"" + src + "\" defer=\"defer\"></script>";
            javascript = "<script language=\"javascript\" type=\"text/javascript\" src=\"" + src + "\"></script>";
            LiteralControl lc = new LiteralControl(javascript);
            page.Header.Controls.Add(lc);
        }
        /// <summary>
        /// 在页面头部添加css链接
        /// </summary>
        /// <param name="page">要加载的页面实例</param>
        /// <param name="href">css全路径</param>
        /// <param name="rel">关联关系</param>
        /// <param name="isIESpecialCss">是否是针对IE的特殊样式</param>
        /// <returns></returns>
        public void AppendCss(Page page, string href, string rel, bool isIESpecialCss)
        {
            string link;
            if (String.IsNullOrEmpty(rel))
                rel = "stylesheet";
            link = "<link href=\"" + href + "\" type=\"text/css\" rel=\"" + rel + "\" />";
            if (isIESpecialCss)
                link = "<!--[if IE]>" + link + "<![endif]-->";
            LiteralControl lc = new LiteralControl(link);
            page.Header.Controls.Add(lc);
        }
        /// <summary>
        /// 在页面头部添加css链接
        /// </summary>
        /// <param name="page">要加载的页面实例</param>
        /// <param name="href">css全路径</param>
        /// <param name="rel">关联关系</param>
        /// <returns></returns>
        public void AppendCss(Page page,string href, string rel)
        {
            AppendCss(page,href, rel, false);
        }
        /// <summary>
        /// 在页面头部添加css链接
        /// </summary>
        /// <param name="page">要加载的页面实例</param>
        /// <param name="href">css全路径</param>
        /// <returns></returns>
        public void AppendCss(Page page,string href)
        {
            AppendCss(page,href, null);
        }
        /// <summary>
        /// 在页面头部添加关键字
        /// </summary>
        /// <param name="page">要加载的页面实例</param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public void AppendKeyword(Page page,string keyword)
        {
            string keywords = "<meta name=\"keywords\" content=\""+keyword+"\" />";
            LiteralControl lc = new LiteralControl(keywords);
            page.Header.Controls.Add(lc);
        }
        /// <summary>
        /// 在页面头部添加描述信息
        /// </summary>
        /// <param name="page">要加载的页面实例</param>
        /// <param name="description">描述信息</param>
        /// <returns></returns>
        public void AppendDescription(Page page, string description)
        {
            string descriptions = "<meta name=\"description\" content=\"" + description + "\" />";
            LiteralControl lc = new LiteralControl(descriptions);
            page.Header.Controls.Add(lc);
        }
        /// <summary>
        /// 在页面头部添加
        /// </summary>
        /// <param name="page">要加载的页面实例</param>
        /// <param name="fileType"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public void AppendContentType(Page page, string fileType, string encoding)
        {
            if (String.IsNullOrEmpty(fileType))
                fileType = "text/html";
            if (String.IsNullOrEmpty(encoding))
                encoding = "utf-8";
            string contentType = "<meta http-equiv=\"Content-Type\" content=\""+fileType+"; charset="+encoding+"\" />";
            LiteralControl lc = new LiteralControl(contentType);
            page.Header.Controls.Add(lc);
        }
          /// <summary>
          /// 设置页面标题
          /// </summary>
          /// <param name="page">页面对象</param>
          /// <param name="title">标题</param>
        public void SetTitle(Page page, string title)
        {
              page.Title = title;
        }
        /// <summary>
        /// 设置页面标题
        /// </summary>
        /// <param name="page">页面对象</param>
        /// <param name="titles">多个标题</param>
        public void SetTitle(Page page, params string[] titles)
        {
              string title = string.Join("-", titles);
              this.SetTitle(page, title);
        }
    }
}
